package cn.chenfei.jfx.login;


import cn.chenfei.jfx.App;
import cn.chenfei.jfx.common.JFUtils;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

/**
 * @author ChenFei
 * @date 2022年4月8日
 */
public class Login_2 extends AnchorPane {

    private StackPane container = new StackPane();
    private StackPane main = new StackPane();

    {
        this.getStylesheets().add(App.class.getResource("css/Login_2.css").toExternalForm());
        this.getChildren().add(container);
        container.getStyleClass().add("container");
        container.getChildren().add(main);
        main.getStyleClass().add("main");
        container.setAlignment(main,Pos.CENTER_RIGHT);
        //拖动
        JFUtils.dragCurrentWindow(container);
    }

    private HBox topHBox = new HBox();
    private Button hideBut = new Button();
    private Button closeBut = new Button();

    {
        topHBox.getChildren().addAll(hideBut, closeBut);
        main.getChildren().add(topHBox);
        main.setAlignment(topHBox, Pos.TOP_RIGHT);
        topHBox.setMaxWidth(0);
        hideBut.setGraphic(new FontIcon(AntDesignIconsOutlined.MINUS));
        closeBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CLOSE));
        hideBut.getStyleClass().add("hide-but");
        closeBut.getStyleClass().add("close-but");
        hideBut.setOnMouseClicked(event -> JFUtils.setIconified(event.getSource(),true));
        closeBut.setOnMouseClicked(event -> JFUtils.closeCurrentWindow(event.getSource()));
    }

    /**
     * 上层按钮
     */
    private VBox content_1 = new VBox();
    private VBox content = new VBox();
    private StackPane titlePane = new StackPane();
    private Button aboutBut = new Button("关于作者 CF");
    private Button helpBut = new Button("帮助？");

    {
        titlePane.getChildren().addAll(aboutBut, helpBut);
        titlePane.setAlignment(aboutBut, Pos.CENTER_LEFT);
        titlePane.setAlignment(helpBut, Pos.CENTER_RIGHT);
        titlePane.getStyleClass().add("title-pane");
        aboutBut.getStyleClass().add("about-but");
        helpBut.getStyleClass().add("help-but");
        content.getStyleClass().add("content");
        content_1.getStyleClass().add("content-1");
        content.getChildren().add(titlePane);
        content.setAlignment(Pos.CENTER);
        content_1.getChildren().add(content);
        main.getChildren().add(content_1);
        main.setAlignment(content_1, Pos.CENTER);
    }

    /**
     * 输入框布局-----------------------------------------------------------------
     */
    private HBox userBox = new HBox();
    private StackPane pswBox = new StackPane();
    private TextField userField = new TextField();
    private PasswordField pswField = new PasswordField();
    private TextField pswText = new TextField();
    private Button pswEye = new Button();
    private Button login = new Button("登录");

    {
        userField.setPromptText("请输入用户名");
        pswField.setPromptText("请输入密码");
        pswText.getStyleClass().add("psw-field");
        pswText.textProperty().bind(pswField.textProperty());
        //
        pswEye.setGraphic(new FontIcon(AntDesignIconsOutlined.EYE_INVISIBLE));
        userBox.getChildren().add(userField);
        pswBox.getChildren().addAll(pswField, pswEye);
        userBox.setHgrow(userField, Priority.ALWAYS);
        pswBox.setAlignment(pswField, Pos.CENTER);
        pswBox.setAlignment(pswEye, Pos.CENTER_RIGHT);
        userField.getStyleClass().add("user-field");
        pswField.getStyleClass().add("psw-field");
        userBox.getStyleClass().add("user-box");
        pswBox.getStyleClass().add("psw-box");
        pswEye.getStyleClass().add("psw-eye");
        login.getStyleClass().add("login");
        content.getChildren().addAll(userBox, pswBox, login);
    }

    /**
     * 底部布局-----------------------------------------------------------------
     */
    private VBox v = new VBox();
    private Button register = new Button("Don't have an account yet ?");
    private Button register_1 = new Button("Create an account");

    {
        register.getStyleClass().add("register");
        register_1.getStyleClass().add("register-1");
        v.getChildren().addAll(register, register_1);
        v.setAlignment(Pos.CENTER);
        content_1.getChildren().addAll(v);
        content_1.setAlignment(Pos.CENTER);
    }

    private VBox messBox = new VBox();

    {
        messBox.getStyleClass().add("mess-box");
        this.getChildren().add(messBox);
        this.setLeftAnchor(messBox, 0.0);
        this.setTopAnchor(messBox, 0.0);
    }

    public Login_2() {
        EventHandler eventUrl = (EventHandler<Event>) e -> new App().getHostServices().showDocument("https://openjfx.cn/");
        register.setOnMouseClicked(eventUrl);
        register_1.setOnMouseClicked(eventUrl);
        aboutBut.setOnMouseClicked(eventUrl);
        helpBut.setOnMouseClicked(eventUrl);
        //密码框显示明文事件
        pswEye.hoverProperty().addListener((observableValue, aBoolean, t1) -> {
            if ("".equals(pswField.getText())) {
                return;
            }
            if (t1) {
                pswEye.setGraphic(new FontIcon(AntDesignIconsOutlined.EYE));
                pswBox.getChildren().set(0, pswText);
                pswBox.setAlignment(pswText, Pos.CENTER);
            } else {
                pswEye.setGraphic(new FontIcon(AntDesignIconsOutlined.EYE_INVISIBLE));
                pswBox.getChildren().set(0, pswField);
                pswBox.setAlignment(pswField, Pos.CENTER);
            }
        });
        //登录点击事件
        login.setOnMouseClicked(event -> {
            event.consume();
            if (Cursor.WAIT.equals(login.getCursor())) {
                return;
            }
            String username = userField.getText();
            String password = pswField.getText();
            System.out.println(String.format("username: %s ; password: %s ;", username, password));
            //
            if (username.trim().equals("")) {
                showMessage("用户名不能为空");
                return;
            }
            if (password.trim().equals("")) {
                showMessage("密码不能为空");
                return;
            }
            login.setCursor(Cursor.WAIT);
        });

    }

    private int i = 0;

    /**
     * 登录提示
     *
     * @param mess
     */
    private void showMessage(String mess) {
        i++;
        Label message = new Label();
        message.setGraphic(new FontIcon(AntDesignIconsOutlined.FROWN));
        message.getStyleClass().add("message");
        message.setText(mess + " " + i);
        messBox.getChildren().add(message);
        //3秒后关闭
        new Thread(() -> {
            try {
                Thread.sleep(2000);
                Platform.runLater(() -> messBox.getChildren().remove(message));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }


}
